<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2019-12-05 00:03:30
 * Desc:
 */

namespace app\admin\controller;

use app\core\storage\COS;
use app\core\log\Log4dd;

class UploadController extends adminAuthController
{

    public function upload()
    {
        $path = trim(input('path/s', ''));
        $acceptExt = [
            'png',
            'jpg',
            'jpeg',
            'gif',
            'webp',
            'mp4',
            'mp3',
            'wav',
            'xls',
            'xlsx',
            'ppt',
            'pdf',
            'word',
            'txt'
        ];
        $file = request()->file("file");
        if ($file) {
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            if (!in_array($ext, $acceptExt)) {
                return $this->errorReturn('非法文件类型');
            }
            $filePath = TEMP_PATH . $ext . '/';
            if (!file_exists($filePath)) {
                mkdir($filePath, 0775, true);
            }

            $fileName = $file->hash('md5');
            $info = $file->move($filePath, $fileName);
            if (!$info) {
                return $this->errorReturn($file->getError());
            }

            //上传到cos对象储存里面
            $fileName = $fileName . '.' . $ext;
            if (empty($path)) {
                return $this->errorReturn('存储path不能为空');
            }

            $local_path = $filePath . $fileName;

            $cosData = Cos::putObject($fileName, $local_path, $path);

            if (!isset($cosData['cos_url'])) {
                return $this->errorReturn('上传失败');
            }

            @unlink($local_path);

            $response_data = [
                'cos_url' => $cosData['cos_url'],
                'cos_path' => $cosData['cos_path'],
            ];
            return $this->successReturn($response_data);
        } else {
            if (isset($_POST['file']) && strpos($_POST['file'], "data:image") !== false) {
                $file = $_POST['file'];
                $pregPattern = "|^data:image/(.*?);base64,(.*)$|";
                if (preg_match($pregPattern, $file, $match)) {
                    $ext = $match[2];
                    $filePath = TEMP_PATH . $ext . '/';
                    if (!file_exists($filePath)) {
                        mkdir($filePath, 0775, true);
                    }
                    $data = base64_decode($match[2]);
                    $fileName = md5($data) . '.' . $match[1];
                    file_put_contents($path . DS . $fileName, $data);

                    $fileName = $fileName . '.' . $ext;

                    if (empty($path)) {
                        return $this->errorReturn('存储path不能为空');
                    }

                    $local_path = $filePath . $fileName;

                    $cosData = Cos::putObject($fileName, $local_path, $path);

                    if (!isset($cosData['cos_url'])) {
                        return $this->errorReturn('上传失败');
                    }

                    $response_data = [
                        'cos_url' => $cosData['cos_url'],
                        'cos_path' => $cosData['cos_path'],
                    ];
                    return $this->successReturn($response_data);
                }
            }
            return $this->errorReturn('没有文件被上传');
        }
    }
}